package com.exampler.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;

@Service
public class SentinelService {
    @SentinelResource(value = "sentinel-test", blockHandler = "blockHandler", fallback = "fallback")
    public String test(long id) {
        /*if (true) {
            throw new RuntimeException("a");
        }*/
        return "test";
    }

    /**
     * Block异常处理函数，用来处理Sentinel限流/熔断等错误
     * 参数最后多一个BlockException，其余与原函数一致
     * （流量规则中要指定的资源名称要与注解中@SentinelResource中value一致才会进入此方法）
     */
    public String blockHandler(long id, BlockException e) {
        e.printStackTrace();
        return "blockHandler";
    }

    /**
     * Fallback函数，用来处理接口中业务代码所有异常
     * 函数签名与原函数一致或加一个 Throwable 类型的参数.
     */
    public String fallback(long id) {
        return "fallback";
    }
}
